package com.hlbbcsr.business.impl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hlbbcsr.business.AccountManager;
import com.hlbbcsr.domain.bean.Address;
import com.hlbbcsr.domain.bean.Customer;

@Service("accountManager")
@Transactional(value = "transactionManager", propagation = Propagation.REQUIRED)
public class AccountManagerImpl extends BaseManager implements
		AccountManager {

	public Customer getCustomerByLoginId(String loginId) throws Exception {

		List<Customer> customerList = getCustomerDAO().findByLoginId(loginId);
		return (customerList != null)? customerList.get(0): null;

	}
	
	public List<Customer> listCustomer() throws Exception {

		return getCustomerDAO().listCgsCustomer();

	}

	@Override
	public void addCustomer(Customer customer) throws Exception {
		if (customer != null) {
			
			// MD5 password
			customer.setPassword(encodeString(customer.getPassword()));
			customer.setCreatedDate(new Date());
			customer.setUpdatedDate(new Date());
			// Call DAO layer to insert customer into database
			getCustomerDAO().addCustomer(customer);

			// Save address
			Address address = new Address();
			address.setAddressName("Address  !!!");
			address.setCreatedDate(new Date());
			address.setUpdatedDate(new Date());
			getAddressDAO().addAddress(address);
		}		
	}
	
};